/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ttfsserver;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import pojo.POJOInfoSharedFile;
import myutil.XMLHelper;

/**
 *
 * @author Tidus Le
 */
public class clsXMLInfoSharedFile
{
    /**
     * Đọc thông tin của các Client từ file xml
     */
    public static void read(ArrayList<POJOInfoSharedFile> arrInfoSharedFiles)
    {
        ArrayList<Hashtable<String,String>> arrElements=XMLHelper.read("InfoSharedFile");
        
        for(Hashtable<String,String> element:arrElements)
        {
            POJOInfoSharedFile infoSF=new POJOInfoSharedFile();
            Enumeration e=element.keys();
              while(e.hasMoreElements())
              {
                  String key=(String)e.nextElement();
                  if(key.equals("fid")==true)
                      infoSF.setFid(element.get(key));
                  else
                      if(key.equals("name")==true)
                        infoSF.setName(element.get(key));
                      else
                          if(key.equals("size")==true)
                            infoSF.setSize(Long.parseLong(element.get(key)));
              }
            arrInfoSharedFiles.add(infoSF);
        }
    }
    
    /**
     * Ghi những thông tin của client xuống đĩa cứng
     */
    public static void write(ArrayList<POJOInfoSharedFile> arrInfoSharedFiles)
    {
        ArrayList<Hashtable<String,String>> arrElements=new ArrayList<Hashtable<String, String>>();
        
        for(POJOInfoSharedFile infoSF:arrInfoSharedFiles)
        {
            Hashtable<String,String> element=new Hashtable<String, String>();
            element.put("fid", infoSF.getFid());
            element.put("name", infoSF.getName());
            element.put("size", String.valueOf(infoSF.getSize()));
            arrElements.add(element);
        }
        XMLHelper.write("InfoSharedFile", arrElements);
    }
}
